package com.igeek.compus.backend.system.interceptor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ActionLogInterceptor implements HandlerInterceptor {

    private final static Logger logger = LogManager.getLogger(ActionLogInterceptor.class.getName());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object action) throws Exception {
        if (logger.isInfoEnabled()) {
            if (action instanceof HandlerMethod) {
                HandlerMethod method = (HandlerMethod) action;
                logger.info("preHandle       action:"
                        + method.getBean().getClass() + " ,method:"
                        + method.getMethod().getName());
            }

        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object action, ModelAndView mv)
            throws Exception {
        if (logger.isInfoEnabled()) {
            if (action instanceof HandlerMethod) {
                HandlerMethod method = (HandlerMethod) action;
                logger.info("postHandle      action:"
                        + method.getBean().getClass() + " ,method:"
                        + method.getMethod().getName());
                logger.info("postHandle          mv:" + mv);
            }

        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object action, Exception exception)
            throws Exception {
        if (logger.isInfoEnabled()) {
            if (action instanceof HandlerMethod) {
                HandlerMethod method = (HandlerMethod) action;
                logger.info("afterCompletion action:"
                        + method.getBean().getClass() + " ,method:"
                        + method.getMethod().getName());
            }

        }
    }



}
